import React from 'react';
import { Spin } from 'antd';
import { isEqual, isEmpty } from 'lodash';
import {WeaErrorPage, WeaLocaleProvider, WeaTools} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import Title from './Title';
import Iframe from './Iframe';
import { setContentviewHeight} from '../../util/common';
//内容组件
class EContent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            tabdata: props.edata.data,
            currentTab: props.edata.currenttab,
            refresh: false,
        }
        this.handleTabData = this.handleTabData.bind(this);
        this.handleRefresh = this.handleRefresh.bind(this);
    }
    handleRefresh(refresh){
        this.setState({
            refresh,
        });   
    }
    componentWillReceiveProps(nextProps){
        if(!isEqual(this.props.edata, nextProps.edata)){
            this.setState({
                tabdata: nextProps.edata.data,
                currentTab: nextProps.edata.currenttab,
            });
        }
    }
    shouldComponentUpdate(nextProps, nextState){
        return !isEqual(this.state, nextState) || !isEqual(this.props, nextProps)
    }
    handleTabData(tabid){
        const { params, isremembertab } = this.props.config;
        const { currentTab } = this.state;
        this.setState({
            currentTab: tabid,
            refresh: true,
        });
        WeaTools.callApi('/api/portal/element/custompagetab', 'POST', {...params, tabid}).then(result => {
            if(tabid === this.state.currentTab){
                this.setState({
                    tabdata: result,
                    refresh: false,
                });
                window.elements_more_params[params.eid] = {moreurl: result.moreUrl};
            }
       });
    }
    componentDidMount(){
        this.props.onRef(this);
    }
    render() {
        const { config, edata, handleRefresh, handleDelete, requestid, fontSizeMp } = this.props;
        const { tabdata, currentTab, refresh } = this.state; 
        const { tabids = [], titles = [], esetting } = edata;
        let { fontStyle } = config.item;
        if(!isEmpty(fontSizeMp)) fontStyle = fontSizeMp;
        esetting.fontStyle = fontStyle;
        const { eid } = config.params;
        const { contentview = {},header } = config.item;
        let cstyle = {height: contentview.style.height,overflow: "auto"};
        if(header.displayPosition && header.displayPosition == 'bottom'){
            if(tabids && tabids.length > 1 || tabids && tabids.length == 1 && esetting.isAlwaysShow=='1'){
                if(contentview.style.height){
                    if(esetting.tabstyle == '1' && esetting.tabvalue){
                        cstyle = setContentviewHeight(contentview.style.height,esetting.tabvalue)
                    }else{
                        cstyle = {height: parseInt(contentview.style.height)-32+"px",overflow: "auto"};
                    } 
                }else {
                    cstyle = {height: contentview.style.height,overflow: "auto"};
                }
                let elementH=parseInt(contentview.style.height)-parseInt($(`#titleContainer_${eid}`).css('height'));
                if(elementH!=parseInt(cstyle.height)){
                    if(elementH<parseInt(cstyle.height)){
                        cstyle.height=elementH+"px";
                    }
                }
            }
        }

        let contentHtml = <div/>
        if(tabdata) {
            const { contenttype, componentName } = tabdata;
            if(contenttype == '1'){
               contentHtml = <Iframe ecId={`${this && this.props && this.props.ecId || ''}_Iframe@fd1p09`} isSetting={config.isSetting} config={config} requestid={requestid} refresh={refresh} data={tabdata} eid={eid} tabid={currentTab} cstyle={contentview.style}/>;
            } else if(contenttype == '2' && componentName){
                try {
                    const { componentModule, componentProps } = tabdata;
                    if(componentModule){
                        contentHtml = <div>{window[componentModule][componentName](componentProps)}</div>
                    }else{
                        contentHtml = <div>{window[componentName](componentProps)}</div>
                    }
                } catch(e) {
                    window.console ? console.log(getLabel(387999,"组件异常："),e) : alert(getLabel(387999,"组件异常：")+e);
                }
            }
        }
        const _titleProps = {
            currentTab,
            config,
            titles,
            tabids,
            handleRefresh,
            handleDelete,
            fontSizeMp,
            tabwidth: esetting.tabwidth,
            methodvalue: esetting.methodvalue,
            isAlwaysShow: esetting.isAlwaysShow,
            tabstyle: esetting.tabstyle,
            tabvalue: esetting.tabvalue,
            handleTabData: this.handleTabData
        }
        return <div>
            <Title ecId={`${this && this.props && this.props.ecId || ''}_Title@sy30ja`} {..._titleProps}/>
            <div className="tabContant" id={`tabcontant_${eid}`} style={{height: cstyle.height,'overflow-y':'auto'}}>
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@gdexrt`} spinning={refresh}>{contentHtml}</Spin>
            </div>
         </div>;
    }
}

class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
            return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@ln4wvn`} msg = { hasErrorMsg ? this.props.error : getLabel(385218,"对不起，该页面异常，请联系管理员！") }/>
        );
    }
}
EContent = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(EContent);
export default EContent;





